package com.dengpp.pdform.holder;


import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dengpp.pdform.R;
import com.dengpp.pdform.adapter.AttributeAdapter;
import com.dengpp.pdform.adapter.MediaAdapter;
import com.dengpp.pdform.base.BaseAttributeViewHolder;
import com.dengpp.pdform.config.AttributeConfig;
import com.dengpp.pdform.model.MediaFile;


import java.util.ArrayList;
import java.util.List;

public class MediaViewHolder extends BaseAttributeViewHolder {
    private TextView tvTitle;
    private RecyclerView recyclerView;
    private Button btnAdd;
    private MediaAdapter mediaAdapter;
    private AttributeConfig currentAttribute;
    private GridLayoutManager gridLayoutManager;

    public MediaViewHolder(@NonNull View itemView) {
        super(itemView);
        tvTitle = itemView.findViewById(R.id.tv_title);
        recyclerView = itemView.findViewById(R.id.recycler_view);
        btnAdd = itemView.findViewById(R.id.btn_add);

        gridLayoutManager = new GridLayoutManager(itemView.getContext(), 3);
        recyclerView.setLayoutManager(gridLayoutManager);
        mediaAdapter = new MediaAdapter();
        recyclerView.setAdapter(mediaAdapter);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void bind(AttributeConfig attribute, boolean isEditMode, AttributeAdapter.OnAttributeChangeListener listener) {
        // 保存当前绑定的属性
        this.currentAttribute = attribute;


        // 动态设置spanCount
        int mediaSpanCount = attribute.getMediaSpanCount();
        if (mediaSpanCount != 3) {
            gridLayoutManager.setSpanCount(attribute.getMediaSpanCount());
            mediaAdapter.setSpanCount(mediaSpanCount); // 传递给adapter
        }


        // 移除旧的点击监听器
        btnAdd.setOnClickListener(null);

        // 使用带星号的标题
        tvTitle.setText(getTitleWithRequiredStar(attribute.getTitle(), attribute.isRequired()));

        List<MediaFile> mediaFiles = attribute.getValue() != null ?
                (List<MediaFile>) attribute.getValue() : new ArrayList<>();
        mediaAdapter.setMediaFiles(mediaFiles);

        if (isEditMode && attribute.isEditable()) {
            btnAdd.setVisibility(View.VISIBLE);
            mediaAdapter.setEditMode(true);

            btnAdd.setOnClickListener(v -> {
                // 确保当前属性仍然匹配
                if (currentAttribute == attribute && listener != null) {
                    listener.onMediaAddRequested(attribute.getKey(), attribute.getMediaFileDialog());
                }
            });

            mediaAdapter.setOnRemoveListener((position, mediaFile) -> {
                // 确保当前属性仍然匹配
                if (currentAttribute == attribute && listener != null) {
                    listener.onMediaRemoveRequested(attribute.getKey(), position);
                }
            });
        } else {
            btnAdd.setVisibility(View.GONE);
            mediaAdapter.setEditMode(false);
        }
    }

    /**
     * 在 ViewHolder 被回收时清理资源
     */
    @Override
    public void onViewRecycled() {
        super.onViewRecycled();
        btnAdd.setOnClickListener(null);
        mediaAdapter.setOnRemoveListener(null);
        currentAttribute = null;
    }
}
